package HORM

import (
	"fmt"
	"strings"
)

// SqlBuilder
//查询：select * from xxx where name='jack' order by xx group by xxx offset xx limit xxx
type SqlBuilder struct {
	builder   *strings.Builder
	column    []string
	tableName string
	where     string
	args      any
	orderBy   string
	groupBy   string
	offset    *int64
	limit     *int64
}

func (s *SqlBuilder) Select(field ...string) *SqlBuilder {
	s.column = field
	return s
}

func (s *SqlBuilder) From(tableName string) *SqlBuilder {
	s.tableName = tableName
	return s
}

// Where 传入SQL条件 name=?,'tom'
func (s *SqlBuilder) Where(format string, a ...any) *SqlBuilder {
	s.where = fmt.Sprintf(format, a...)
	return s
}

// OrderBy
// xxx ASC(默认) | DESC
func (s *SqlBuilder) OrderBy(field ...map[string]string) *SqlBuilder {
	var res strings.Builder
	for index, item := range field {
		for k, v := range item {
			res.WriteString(k)
			res.WriteString("")
			res.WriteString(v)
			if index == 0 {
				res.WriteString(",")

			}
		}
	}
	s.orderBy = res.String()
	return s
}
func (s *SqlBuilder) GroupBy(field string) *SqlBuilder {
	s.groupBy = field
	return s
}

func (s *SqlBuilder) Limit(offset, limit int64) *SqlBuilder {
	s.offset = &offset
	s.limit = &limit
	return s
}

func (s *SqlBuilder) Query() string {
	s.builder.WriteString("select")
	for index, value := range s.column {
		s.builder.WriteString(value)
		if index != len(s.column)-1 {
			s.builder.WriteString(",")
		}
	}
	s.builder.WriteString(fmt.Sprintf("from %s", s.tableName))

	return ""
}

func main() {

}
